package avicit.bdp.dcs.detail.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.common.utils.enums.JobType;
import avicit.bdp.dcs.detail.dao.TaskTableDetailDao;
import avicit.bdp.dcs.detail.dto.TableDetailDto;
import avicit.bdp.dcs.detail.dto.TaskTableDetailDTO;
import avicit.bdp.dcs.job.dto.JobDTO;
import avicit.bdp.dcs.job.service.DcsJobService;
import avicit.bdp.dcs.tools.enums.ProcessMode;
import avicit.bdp.dcs.tools.enums.TaskDetailStatus;
import avicit.bdp.dcs.tools.enums.TransferStatus;
import avicit.bdp.dcs.utils.DcsErrorCode;
import avicit.platform6.core.rest.msg.QueryRespBean;
import com.github.pagehelper.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间：2020-08-03 11:33
 * @类说明：
 * @修改记录：
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TaskTableDetailService extends BaseService<TaskTableDetailDao, TaskTableDetailDTO> {

    private static final Logger logger = LoggerFactory.getLogger(TaskTableDetailService.class);

    @Autowired
    private TaskTableDetailDao taskTableDetailDao;
    @Autowired
    private DcsJobService jobService;

    public List<TableDetailDto> getTaskTableDetailListByJobId(String jobId) {
        return taskTableDetailDao.getTaskTableDetailListByJobId(jobId);
    }

    /**
     * 获取表数据明细信息
     * @param jobId
     * @param srcTableName
     * @param currentPage
     * @param pageSize
     * @return
     */
    public QueryRespBean<TableDetailDto> getTaskTableDetailListByJobIdPage(String jobId, String srcTableName, Integer currentPage, Integer pageSize) {
        QueryRespBean<TableDetailDto> queryRespBean = new QueryRespBean<>();
        com.github.pagehelper.PageHelper.startPage(currentPage, pageSize);
        Page<TableDetailDto> tableDetailList = taskTableDetailDao.getTaskTableDetailListByJobIdPage(jobId, srcTableName);

        JobDTO jobDTO = jobService.selectByPrimaryKey(jobId);
        CheckParaUtils.checkDbObjectValidity(jobDTO, logger, String.format(DcsErrorCode.JOB_NULL.getDesc(), jobId));

        if (CollectionUtils.isNotEmpty(tableDetailList)) {
            for(TableDetailDto tableDetailDto : tableDetailList){
                if (jobDTO.getJobType() == JobType.SEMISTRUCTURE.getCode()) {
                    tableDetailDto.setSrcTableName(jobService.getSyncFilePath(tableDetailDto.getSrcTableName(), jobDTO.getSelectedFile()));
                }
            }
        }
        queryRespBean.setResult(tableDetailList);
        BdpLogUtil.log4Query(queryRespBean);
        return queryRespBean;
    }

    /**
     * 查询创建表进度
     * @param jobDTO
     * @return
     */
    public String getCreateTableProgressByJobId(JobDTO jobDTO) {
        if(jobDTO.getJobStatus()== TransferStatus.DRAFT.getCode()){
            return "0%";
        }

        int createSuccessNum = 0;
        float progress = 0F;

        List<TableDetailDto> tableDetailDtoList = this.getTaskTableDetailListByJobId(jobDTO.getId());
        if (CollectionUtils.isNotEmpty(tableDetailDtoList)) {
            for(TableDetailDto tableDetailDto : tableDetailDtoList){
                if(TaskDetailStatus.COMPLETED.getCode()==tableDetailDto.getTaskStatus()){
                    createSuccessNum += 1;
                }
            }

            if(tableDetailDtoList.size()>0){
                progress = createSuccessNum * 1f / tableDetailDtoList.size();
            }
        }

        // 选择保留目标表，创建表进度默认100%
        if(jobDTO.getJobStatus()== TransferStatus.TRANSFERING.getCode() ||
                jobDTO.getJobStatus()== TransferStatus.TRANSFER_FAIL.getCode() ||
                jobDTO.getJobStatus()== TransferStatus.COMPLETED.getCode()){
            if(jobDTO.getProcessMode()== ProcessMode.RETAIN_DST_TABLE.getCode()){
                progress = 1;
            }
        }
        return Math.round(progress * 100) + "%";
    }

}
